package com.tedu.egmvc3.controller;

import com.tedu.egmvc3.base.response.StatsTest;
import com.tedu.egmvc3.base.response.StatusCodeTest;
import com.tedu.egmvc3.mapper.UserMapper;
import com.tedu.egmvc3.pojo.dto.param.UserAddParam;
import com.tedu.egmvc3.pojo.dto.param.UserUpdateParam;
import com.tedu.egmvc3.pojo.entity.User;
import com.tedu.egmvc3.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private  UserMapper userMapper;

    @PostMapping("add")
    public StatsTest add(UserAddParam userAddParam){
        System.out.println("添加用户："+userAddParam);
        User user=new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insert(user);
        return StatsTest.ok();
    }
    @GetMapping("list")
    public StatsTest list(){
        System.out.println("查询用户列表");
        List<UserListVO>userListVOS=userMapper.list();
        return StatsTest.ok(userListVOS);
    }
    @PostMapping("delete")
    public StatsTest delete(Long id){
        System.out.println("删除用户："+id);
        int num=userMapper.delete(id);
        if (num>0){
            return StatsTest.ok();
        }else {
            return new StatsTest(StatusCodeTest.OPERATION_FAILED);
        }
    }
    @PostMapping("update")
    public StatsTest  updata(UserUpdateParam userUpdateParam){
        System.out.println("更新用户："+userUpdateParam);
        User user=new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int num=userMapper.update(user);
        if (num>0){
            return StatsTest.ok();
        }else {
            return new StatsTest(StatusCodeTest.OPERATION_FAILED);
        }
    }
}
